home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / showfiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  2.3 KB  |  142 lines

  1. /*
  2.  * Copyright (C) 1990-1992 by Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. #include    <stdio.h>
  15.  
  16. #include    "vg.h"
  17.  
  18. void
  19. showFiles(
  20.     list_t    *filelist
  21.     )
  22. {
  23.     list_t    *f    = filelist;
  24.  
  25.     /*
  26.      * find first selected file
  27.      */
  28.     for (f = filelist; f != NULL; f = f->next)
  29.     if (f->flags & 2)
  30.         break;
  31.  
  32.     while (f != NULL)
  33.     {
  34.     switch (showFile(f->name))
  35.     {
  36.         case F_NEXT:
  37.         for (f = f->next; f != NULL; f = f->next)
  38.             if (f->flags & 2)
  39.             break;
  40.         break;
  41.  
  42.         case F_PREV:
  43.         for (f = f->prev; f != NULL; f = f->prev)
  44.             if (f->flags & 2)
  45.             break;
  46.         break;
  47.  
  48.         case F_REDRAW:
  49.         continue;
  50.  
  51.         case F_ESC:
  52.         return;
  53.     }
  54.     }
  55. }
  56.  
  57. int
  58. showFile(
  59.     char    *fname
  60.     )
  61. {
  62.     char    iobuf[BUFSIZ*8];
  63.     FILE    *fp;
  64.     int        r;
  65.   
  66.     if ((fp = fopen(fname, "r")) == NULL)
  67.     return imageError("can't open %s", fname);
  68.  
  69.     setvbuf(fp, iobuf, _IOFBF, BUFSIZ*8);
  70.  
  71.  
  72.     switch (getFileType(fname, fp))
  73.     {
  74.     case F_GIF:
  75.         r = gifRead(fname, fp);
  76.         break;
  77.  
  78.     case F_JPEG:
  79.         r = jpegRead(fname, fp);
  80.         break;
  81.  
  82.     case F_PCD_O:
  83.     case F_PCD_I:
  84.         r = pcdRead(fname, fp);
  85.         break;
  86.  
  87.     default:
  88.         r = imageError("can't determine file type of %s", fname);
  89.         break;
  90.     }
  91.  
  92.     fclose(fp);
  93.  
  94.     return r;
  95. }
  96.  
  97. getFileType(
  98.     char    *fname,
  99.     FILE    *fp
  100.     )
  101. {
  102.     unsigned char buf[512];
  103.     int        n;
  104.  
  105.     if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf))
  106.     return -1;
  107.  
  108.     rewind(fp);
  109.  
  110.     if (strncmp(buf, "GIF8", 4) == 0)
  111.     return F_GIF;
  112.  
  113.     if (strncmp(&buf[128], "GIF8", 4) == 0)
  114.     {
  115.     fseek(fp, 128L, 0);
  116.     return F_GIF;
  117.     }
  118.  
  119.     if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff)
  120.     return F_JPEG;
  121.  
  122.     if (buf[128] == 0xff && buf[129] == 0xd8 && buf[130] == 0xff)
  123.     {
  124.     fseek(fp, 128L, 0);
  125.     return F_JPEG;
  126.     }
  127.  
  128.     if (strcmp(buf, "PCD_OPA") == 0)
  129.     return F_PCD_O;
  130.  
  131.     if (fseek(fp, 2048L, 0) == 0)
  132.     {
  133.     fread(buf, 1, sizeof(buf), fp);
  134.     rewind(fp);
  135.  
  136.     if (strcmp(buf, "PCD_IPI") == 0)
  137.         return F_PCD_I;
  138.     }
  139.  
  140.     return 0;
  141. }
  142.